package com.cw.demo.dao.impl;

import com.cw.demo.dao.DeptDao;
import com.cw.demo.model.Dept;
import com.cw.demo.utils.DbUtils;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *  使用mysql的数据库进行的 CRUD
 */
public class DeptDaoImpl implements DeptDao {

    public boolean saveDept(Dept dept){
        Connection connection = DbUtils.getInstance().getConnection();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("insert into dept(id, dname, loc) values (?,?,?)");
            preparedStatement.setInt(1,dept.getId());
            preparedStatement.setString(2,dept.getdName());
            preparedStatement.setString(3,dept.getLoc());
            preparedStatement.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

    public boolean updateDept(Dept dept){

        Connection connection = DbUtils.getInstance().getConnection();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("update dept set dname = ? , loc = ? where  id = ?");
            preparedStatement.setString(1,dept.getdName());
            preparedStatement.setString(2,dept.getLoc());
            preparedStatement.setInt(3,dept.getId());
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteDept(int id){
        Connection connection = DbUtils.getInstance().getConnection();

        try {
            PreparedStatement preparedStatement = connection.prepareStatement("delete from  dept where  id = " + id);
            preparedStatement.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 查询操作。根据查询条件  dname和loc进行模糊查询
     * @param param
     * @return
     */
    public List<Dept> findByParam(Dept param){
        String sql = "select * from dept where 1 = 1";
        //字符串的操作工具类：可以帮助判断字符串是否为null或''
        if(param != null){
            if(StringUtils.isNotEmpty(param.getdName())){
                sql += " and dname like '%"+param.getdName()+"%'";
            }
            if (StringUtils.isNotEmpty(param.getLoc())){
                sql += " and loc like '%"+param.getLoc()+"%'";
            }
        }

        //将查询结果进行重新封装，返回集合bean对象
        Connection connection = DbUtils.getInstance().getConnection();
        //创建一个返回结果集类型的空对象
        List<Dept> list = new ArrayList<Dept>();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Dept dept = new Dept();
                dept.setId(resultSet.getInt("id"));
                dept.setdName(resultSet.getString("dname"));
                dept.setLoc(resultSet.getString("loc"));

                list.add(dept);
            }
//            return list;
        } catch (SQLException e) {
            e.printStackTrace();
//            return list;
        }
        return list;
    }


    /**
     * 根据主键ID查询对象信息
     * @param id
     * @return
     */
    public Dept findById(int id){
        Connection connection = DbUtils.getInstance().getConnection();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("select  * from dept where  id = " + id);
            ResultSet resultSet = preparedStatement.executeQuery();//此时，数据返回的肯定只有一条或0条
            Dept dept = new Dept();
            boolean result = resultSet.next();
            if (result){
                dept.setId(resultSet.getInt("id"));
                dept.setdName(resultSet.getString("dname"));
                dept.setLoc(resultSet.getString("loc"));
            }
            return dept;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }
}
